package root.electronclass.electronclass.grade.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Shader
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.View
import androidx.core.graphics.toColorInt
import java.util.Locale

/**
 * 圆角柱状图图表
 * @author Lokiy
 * @date 2025/6/11 21:23
 */
class RoundedBarChartView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : View(context, attrs) {

    private val items = mutableListOf<BarChartItem>()
    private val barPaint = Paint(Paint.ANTI_ALIAS_FLAG)

    private val yColor = "#73000000".toColorInt()
    private val lineColor = "#10000000".toColorInt()
    private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        textSize = 8f
        textAlign = Paint.Align.CENTER
        typeface = Typeface.DEFAULT_BOLD
    }
    private val labelPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = "#FF093B5D".toColorInt()
        textSize = 12f
        textAlign = Paint.Align.CENTER
    }
    private val percentPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = "#FF19C5FF".toColorInt()
        textSize = 12f
        textAlign = Paint.Align.CENTER
        typeface = Typeface.DEFAULT_BOLD
    }

    private val startColor = "#FF19C5FF".toColorInt()
    private val endColor = "#0037E4FF".toColorInt()

    init {
        if (isInEditMode) {
            setData(
                listOf(
                    BarChartItem("语文", 100f),
                    BarChartItem("数学", 94.1f),
                    BarChartItem("英语", 92f),
                    BarChartItem("化学", 90f),
                    BarChartItem("物理", 89f),
                    BarChartItem("体育", 67.2f),
                    BarChartItem("政治", 54f)
                )
            )
        }
    }

    fun setData(data: List<BarChartItem>) {
        items.clear()
        items.addAll(data)
        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (items.isEmpty()) return

        val barWidth = width / (items.size) / 2f
        val barSpace = barWidth / 3 * 2
        val labelY = height - 2f
        val bottomMargin = 2 + 12f + 8f
        val radius = barWidth / 2
        val topMargin = 12f + 10f
        val maxBarHeight = height - bottomMargin - topMargin

        val startX = (width - (items.size * barWidth + (items.size - 1) * barSpace)) / 2f

        for ((i, item) in items.withIndex()) {
            val percent = item.percent.coerceIn(0f, 100f)
            val barHeight = maxBarHeight * percent / 100f
            val left = startX + i * (barWidth + barSpace)
            val right = left + barWidth
            val bottom = height - bottomMargin
            val top = bottom - barHeight

            // 渐变色
            val shader = LinearGradient(0f, top, 0f, bottom, startColor, endColor, Shader.TileMode.CLAMP)
            barPaint.shader = shader

            // 画圆角柱子
            canvas.drawRoundRect(left, top, right, bottom, radius, radius, barPaint)

            // 百分比
            val percentText = String.format(Locale.getDefault(), "%.1f%%", percent)
            canvas.drawText(percentText, left + barWidth / 2, top - 2f, percentPaint)

            // 科目名
            canvas.drawText(item.label, left + barWidth / 2, labelY, labelPaint)
        }
        //画百分比刻度
        var dy = 0f
        textPaint.color = yColor
        repeat(5) {
            canvas.drawText((it * 25).toString(), startX - 24f, labelY - 12f + dy, textPaint)
            dy -= height / 4f - 10f
        }

        val lineY = labelY - 16f
        val lineStartX = startX - 12f
        val lineEndX = lineStartX + (barWidth + barSpace) * items.size
        textPaint.color = lineColor
        canvas.drawLine(lineStartX, lineY, lineEndX, lineY, textPaint)
    }
}

data class BarChartItem(
    val label: String,    // 科目名
    val percent: Float    // 百分比 0~100
)